const DB = require('../db/db');

module.exports = {
    // 查询所有的分院信息
    async selectbranch() {
        let result = await DB.select('branch', {})
        return { msg: '查询成功', code: 0, data: result };
    },
    // 查询所有的部门信息
    async selectdepartment() {
        let result = await DB.select('department', {})
        return { msg: '查询成功', code: 0, data: result };
    },
    // 通过分院查询
    async selectbranchfromdoctor(param) {
        let sql = `
        SELECT 
        a.id,
        a.img,
        a.introduce,
        a.name,
        a.title,
        b.name AS branch_name,
        c.name AS department_name,
		a.branch_id 
    FROM 
        doctor AS a
    LEFT JOIN 
        branch AS b ON a.branch_id = b.id
    LEFT JOIN 
        department AS c ON a.department_id = c.id
		WHERE
		a.branch_id = ${param.branch_id};
    `;
        let result = (await DB.execute(sql))[0]
        console.log(result);
        return { msg: '查询成功', code: 0, data: result };
    },
    // 通过分院查询
    async selectdepartmentfromdoctor(param) {
        let sql = `
        SELECT 
        a.id,
        a.img,
        a.introduce,
        a.name,
        a.title,
        b.name AS branch_name,
        c.name AS department_name,
        a.department_id
    FROM 
        doctor AS a
    LEFT JOIN 
        branch AS b ON a.branch_id = b.id
    LEFT JOIN 
        department AS c ON a.department_id = c.id
	WHERE
		a.department_id = ${param.department_id};
    `;
        let result = (await DB.execute(sql))[0]
        console.log(result);
        return { msg: '查询成功', code: 0, data: result };
    },
    // 查询全部
    async select() {
        let sql = `SELECT 
        a.id,
        a.img,
        a.introduce,
        a.name,
        a.title,
        b.name AS branch_name,
        b.id AS branch_id,
        c.id AS department_id,
        c.name AS department_name
    FROM 
        doctor AS a
    LEFT JOIN 
        branch AS b ON a.branch_id = b.id
    LEFT JOIN 
        department AS c ON a.department_id = c.id;`
        let result = await DB.execute(sql)
        return { msg: '查询成功', code: 0, data: result[0] };
    },
    // 新增医生
    async add(param) {
        let result = await DB.insert('doctor', param)
        if (result.affectedRows > 0) {
            return { msg: '医生新增成功', code: 0, data: result };
        }
    },
    // 删除
    async del(param) {
        console.log(param)
        let result = await DB.del('doctor', param)
        return {
            msg: '医生删除成功', code: 0, data: result
        }
    },
    //更新
    async update(param) {
        console.log(param)
        let result = await DB.update('doctor', {
            updateFields: {
                name: `"${param.name}"`,
                title: `"${param.title}"`,
                department_id: param.department_id,
                branch_id: param.branch_id,
                introduce: `"${param.introduce}"`,
                img: `"${param.img}"`,
            },
            where_and: [`id=${param.id}`]
        });
        return { msg: '修改成功', code: 0, data: result };
    },
    // 通过姓名查询
    async selectbyname(param) {
        let sql = `
        SELECT 
        a.id,
        a.img,
        a.introduce,
        a.name,
        a.title,
        b.name AS branch_name,
        c.name AS department_name,
        a.branch_id 
    FROM 
            doctor AS a
    LEFT JOIN 
            branch AS b ON a.branch_id = b.id
    LEFT JOIN 
            department AS c ON a.department_id = c.id
    WHERE
    a.name = '${param.name}';
        `;
        let result = (await DB.execute(sql))[0]
        return { msg: '查询成功', code: 0, data: result };

    }
}